package com.capgemini.fs.presentation.forum.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Forum;
import com.capgemini.fs.app.ekms.model.Topic;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.service.ForumService;
import com.capgemini.fs.app.ekms.service.TopicService;
import com.capgemini.fs.app.ekms.service.UserService;
import com.capgemini.fs.presentation.forum.web.form.CreateTopicForm;

public class CreateTopicAction extends Action{

	private TopicService topicService=null;
	private ForumService forumService=null;
	private UserService userService=null;
	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public ForumService getForumService() {
		return forumService;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	


	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CreateTopicForm createTopicForm=(CreateTopicForm)form;
		Topic newTopic=new Topic();
		newTopic.setSubject(createTopicForm.getNewTopicSubject());
		newTopic.setContent(createTopicForm.getNewTopicContent());
		newTopic.setStatus(Constants.TOPIC_STATUS_ACTIVE);
		//Set initiate value of ViewsCount as 0 when topic is first created
		newTopic.setViewsCount(0);
		User author=userService.getUserByName(request.getSession().getAttribute("username").toString());
		Forum forum=forumService.getForumById(createTopicForm.getForumId());
		topicService.createTopic(newTopic, author, forum);
		request.setAttribute("topic", newTopic);
		return mapping.findForward("succ");
	}
	
}
